<?php

namespace app\api\model\cps;



use app\api\model\ExtendConsume;
use app\api\model\UserConsumeLog;
use app\common\model\User;
use app\shopapi\model\ExtendConsumeShop;
use fast\Http;

class Common
{

    protected $apikey = 'dL4aXkyMfQIqylX3zDMP46Vxd3efJj4g';

    public $cps_arr = ['4'=>'京东','14'=>'唯品会','3'=>'拼多多','32'=>'淘宝'];

    public $cps_type_arr = [0=>'京东',2=>'唯品会',1=>'拼多多',7=>'淘宝'];

    public function stickerboard($content)
    {
        $api = "http://api.act.jutuike.com/union/wn_convert";

        $data = [
            'apikey'=>$this->apikey,
            'pub_id'=>185736,
            'content'=>urlencode($content)
        ];

        $res = Http::sendRequest($api,$data);

        $result = json_decode($res['msg'],true);

        if ($result['code']==1){
            return $result['data'];
        } else {
            throw new \Exception($result['msg']);
        }
    }

    //转链
    public function changeUrl($user_id,$content)
    {
        //判断是否包含淘宝
        $reg_exUrl_taobao = "/.*taobao.com|tmall.com|tmall.hk|tb.cn.*/";

        if (preg_match($reg_exUrl_taobao,$content)){
            $tbk_model = new \app\api\model\cps\Tbk();

            $res = $tbk_model->urlprivilege($content);
            if ($res['code']==0){
                throw new \Exception($res['msg']);
            }

            return [
                'type'=>'taobao',
                'data'=>$res['data']
            ];
        } else {
            $stickerboard = $this->stickerboard($content);

            $source = $stickerboard['source'];
            $goodsId = $stickerboard['goodsId'];
            if ($source=='vip'){
                $wph_model = new Wph();
                $res = $wph_model->urlPrivilege($content,$user_id);
                if ($res['code']==1){
                    return [
                        'type'=>$source,
                        'data'=>$res['data']
                    ];
                } else {
                    throw new \Exception($res['msg']);
                }
            }

            $api = "http://api.act.jutuike.com/union/convert";

            $data = [
                'apikey'=>$this->apikey,
                'pub_id'=>185736,
                'goodsId'=>$goodsId,
                'sid'=>$user_id.'userid',
                'source'=>$source//来源 jd-京东,vip-唯品会,pdd-拼多多,taobao-淘宝 默认vip
            ];

            $res = Http::sendRequest($api,$data);

            $result = json_decode($res['msg'],true);

            if ($result['code']==1){
                $data = $result['data'];
                //$data['source'] = $source;
                $data['original_commission'] = $data['commission'];
                $data['commission'] = round($data['commission']*0.56,2);
                //return $data;
                return [
                    'type'=>$source,
                    'data'=>$data
                ];
            } else {
                throw new \Exception($result['msg']);
            }
        }


    }

    //查询未结算的订单
    public function unliquidated()
    {
        $lists = UserConsumeLog::where(['consume_type'=>2,'is_to_account'=>0,'is_common'=>1])
            ->field('pay_time')->select();

        foreach ($lists as $k=>$v){
            $start = $v['pay_time']-3600;
            $end = $v['pay_time'];
            $this->order(1,$start,$end,1);
            $this->order(1,$start,$end,2);
        }
    }

    //订单
    public function order($type=0,$start='',$end='',$query_type)
    {
        $api = "http://api.jutuike.com/union/orders";

        $start_time = date('Y-m-d H:i:s',time()-3600);
        $end_time = date('Y-m-d H:i:s',time());
        if ($type==1){
            $start_time = date('Y-m-d H:i:s',$start);
            $end_time = date('Y-m-d H:i:s',$end);
        }

        $data = [
            'apikey'=>$this->apikey,
            'pub_id'=>185736,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'query_type'=>$query_type
        ];
        //dump($data);
        $res = Http::sendRequest($api,$data);

        $result = json_decode($res['msg'],true);

        if ($result['code']==1){
            if (isset($result['data']['data'])){
                $data = $result['data']['data'];

                foreach ($data as $k=>$v){

                    $promotion_amount = $v['jtk_share_fee'];
                    $commission = $promotion_amount*0.56;
                    $user = User::get($v['sid']);
                    if ($v['brand_id']==3){
                        $cps_type = 1;
                        $shop_name = '拼多多';
                    } elseif ($v['brand_id']==4){
                        $cps_type = 0;
                        $shop_name = '京东';
                    } elseif ($v['brand_id']==14){
                        $cps_type = 2;
                        $shop_name = '唯品会';
                    } elseif ($v['brand_id']==32){
                        $cps_type = 7;
                        $shop_name = '淘宝';
                    } elseif ($v['brand_id']==2){
                        $cps_type = 8;
                        $shop_name = '飞猪';
                    }
                    if ($user){
                        //增加消费记录
                        $consume_log_info = UserConsumeLog::get([ 'user_id'=>$user->id, 'order_no'=>$v['order_sn']]);
                        if (!$consume_log_info){
                            $consume_log_model = new UserConsumeLog();

                            $consume_log_data = [
                                'user_id'=>$user->id,
                                'title'=>$v['order_title'],
                                'shop_id'=>0,
                                'shop_name'=>$shop_name,
                                'image'=>isset($v['icon'])?$v['icon']:'',
                                'order_no'=>$v['order_sn'],
                                'pay_time'=>strtotime($v['pay_time']),
                                'pay_price'=>$v['pay_price'],
                                'consume_type'=>2,
                                'cps_type'=>$cps_type,
                                'goods_id'=>'',
                                'commission'=>$commission,
                                'total_commission'=>$promotion_amount,
                                'order_status_name'=>'已付款',
                                'is_common'=>1
                            ];
                            $consume_log_model->save($consume_log_data);
                            UserConsumeLog::lockMemberConsumeCalculate($user->id,$consume_log_model->total_commission,$consume_log_model->consume_id,0);
                            UserConsumeLog::newrecommendShopConsumeCalculate($user->id,0,$consume_log_model->total_commission,$consume_log_model->consume_id,2);
                        } else {
                            if ($v['status']==3&&$consume_log_info->is_to_account==0){
                                //发放消费记录的收益
                                UserConsumeLog::sendCommission($consume_log_info->order_no);
                            } elseif ($v['status']==4&&$consume_log_info->is_refund==0){
                                $consume_log_info->commission = 0;
                                $consume_log_info->order_status_name = '退款成功';
                                $consume_log_info->is_refund = 1;
                                $consume_log_info->save();
                                ExtendConsume::update(['commission'=>0],['consume_log_id'=>$consume_log_info->consume_id]);
                                ExtendConsumeShop::update(['commission'=>0],['consume_log_id'=>$consume_log_info->consume_id]);
                            }
                        }
                    }

                }
            }

            return $result['data'];
        } else {
            return [];
            //throw new \Exception($result['msg']);
        }
    }
}